package com.gitee.zw.irmp.report.service.impl;

import com.gitee.zw.irmp.report.dao.ReportDefRepository;
import com.gitee.zw.irmp.report.dao.ReportGroupRepository;
import com.gitee.zw.irmp.report.entity.ReportGroup;
import com.gitee.zw.irmp.report.service.IReportManageService;
import com.gitee.zw.irmp.report.vo.ReportCat;
import com.gitee.zw.irmp.report.vo.ReportItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ReportManageServiceImpl implements IReportManageService {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    ReportDefRepository reportRepository;
    @Autowired
    ReportGroupRepository groupRepository;

    /**
     * 读取报表列表
     */
    @Override
    public List<ReportItemVo> getReportListTree(String categoryCode, String status, String userName) {
        List<ReportItemVo> result = new ArrayList<>();
        ReportGroup group = groupRepository.findById(categoryCode).get();
        log.debug("children of {} is :{}", categoryCode, group.getSubGroups().size());
        List<String> favorReports = reportRepository.getFavorReports(userName);
        List<ReportItemVo> groups = new ArrayList<>();
        for (ReportGroup g : group.getSubGroups()) {
            ReportItemVo item = new ReportItemVo();
            item.setReportCode(g.getGroupCode());
            item.setReportName(g.getGroupName());
            groups.add(item);
            item.setChildren(new ArrayList<>(g.getReports().size()));
            g.getReports().forEach(rpt -> {
                if (status == null || status.equals(rpt.getStatus())) {
                    ReportItemVo item1 = new ReportItemVo();
                    item1.setReportCode(rpt.getReportCode());
                    item1.setReportName(rpt.getReportName());
                    if (favorReports.contains(rpt.getReportCode())) {
                        item1.setIsFavor(Boolean.TRUE);
                    }
                    item.getChildren().add(item1);
                }
            });
        }
        return groups;
/*        List<ReportCat> cats = getReportList(categoryCode, status, userName);
        if (cats != null && cats.size() > 0) {
            //首先获取报表大类
            for (ReportCat item : cats) {
                if (item.getParentId() == null && item.getReportCode() == null) {
                    List<ReportItemVo> subLevel1Array = new ArrayList<>();
                    //首先获取类别列表，再读取该类别所属报表
                    for (ReportCat item1 : cats) {
                        if (item1.getParentId() != null && item1.getParentId().equals(item.getId())
                                && item1.getReportCode() == null) {
                            //类别ID
                            String parentId = String.valueOf(item1.getId());
                            List<ReportItemVo> subArray = new ArrayList<>();
                            for (ReportCat item2 : cats) {
                                if (item2.getParentId() != null && String.valueOf(item2.getParentId()).equals(parentId)
                                        && item2.getReportName() != null) {
                                    ReportItemVo report = new ReportItemVo();
                                    report.setKey(item2.getId());
                                    report.setValue(report.getKey());
                                    report.setParentId(item2.getParentId());
                                    report.setReportCode(item2.getReportCode());
                                    report.setTitle(item2.getReportName());
                                    report.setFavorites(StringUtils.isEmpty(item2.getUserName()) ? "F" : "T");
                                    //report.setLeaf(true);
                                    subArray.add(report);
                                }
                            }
                            ReportItemVo category = new ReportItemVo();

                            category.setKey(item1.getId());
                            category.setValue(category.getKey());
                            category.setParentId(item1.getParentId());
                            category.setReportCode(item1.getReportCode());
                            category.setTitle(item1.getReportName());
                            category.setFavorites(StringUtils.isEmpty(item1.getUserName()) ? "F" : "T");
//                            category.setActive(subLevel1Array.size() == 0 ? true : false);
//                            category.setDisabled(false);
                            category.setChildren(subArray);
//                            category.setExpanded(true);
                            subLevel1Array.add(category);
                        }
                    }

                    ReportItemVo category0 = new ReportItemVo();

                    category0.setKey(item.getId());
                    category0.setValue(category0.getKey());
                    category0.setParentId(item.getParentId());
                    category0.setReportCode(item.getReportCode());
                    category0.setTitle(item.getReportName());
                    category0.setFavorites(StringUtils.isEmpty(item.getUserName()) ? "F" : "T");
                    category0.setActive(subLevel1Array.size() == 0 ? true : false);
                    category0.setDisabled(false);
                    category0.setChildren(subLevel1Array);
                    category0.setExpanded(true);
                    result.add(category0);
                }
            }
        }
        return result;*/
    }

    public List<ReportCat> getReportList(String categoryCode, String status, String userName) {
        List<Object[]> reports = reportRepository.findByReportCatAndFavor(userName, categoryCode);
        ArrayList<ReportCat> reportCats = new ArrayList<>(reports.size());
        reports.forEach(r -> {
            ReportCat cat = new ReportCat();
            //id, t.parentId, t.reportCode, t.reportName, t.seq, f.userNam
            cat.setId((Long) r[0]);
            cat.setParentId((Long) r[1]);
            cat.setReportCode((String) r[2]);
            cat.setReportName((String) r[3]);
            cat.setSeq((Long) r[4]);
            cat.setUserName((String) r[5]);
            reportCats.add(cat);
        });
        return reportCats;
    }
}
